package admin.iss.nus.edu.sg;

import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.dynamodb.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodb.model.ComparisonOperator;
import com.amazonaws.services.dynamodb.model.Condition;
import com.amazonaws.services.dynamodb.model.DeleteItemRequest;
import com.amazonaws.services.dynamodb.model.DeleteItemResult;
import com.amazonaws.services.dynamodb.model.Key;
import com.amazonaws.services.dynamodb.model.PutItemRequest;
import com.amazonaws.services.dynamodb.model.PutItemResult;
import com.amazonaws.services.dynamodb.model.QueryRequest;
import com.amazonaws.services.dynamodb.model.QueryResult;

import sg.edu.nus.iss.CONFIG;
import sg.edu.nus.iss.stockex.LinearRegression;

public class LinearRegressionTable implements DynamoDBTableInf<LinearRegression> {
	private AmazonDynamoDBClient dynamoDB = DynamoDB.client();
	@Override
	public Map<String, AttributeValue> newItem(LinearRegression item) {
		Map<String, AttributeValue> map = new HashMap<String, AttributeValue>();
		map.put("code", new AttributeValue(item.getCode()));
		map.put("date", new AttributeValue(item.getDate()));
		map.put("slope", new AttributeValue().withN(Double.toString(item.getSlope())));
		map.put("intercept", new AttributeValue().withN(Double.toString(item.getIntercept())));
		return map;
	}

	@Override
	public boolean Add(LinearRegression item) {
		boolean status = false;
		 try{
		 Map<String,AttributeValue> map = newItem(item);
		 PutItemRequest putItemRequest = new PutItemRequest(CONFIG.LINEAR_REGRESSION_TABLE_NAME, map);
		 PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
		 System.out.println("Result: " + putItemResult);
		 if(putItemResult!=null)
		 {
			 status =  true;
		 }
		 }catch (Exception e) {
			 e.printStackTrace();
			// TODO: handle exception
		}
		
		 return status;
	}

	@Override
	public boolean Delete(LinearRegression item) {
		boolean status = false;
		try{
			Key key = new Key();
			key.setHashKeyElement(new AttributeValue(item.getCode()));
			key.setRangeKeyElement(new AttributeValue(item.getDate()));
			DeleteItemRequest deleteItemRequest = new DeleteItemRequest().withTableName(CONFIG.LINEAR_REGRESSION_TABLE_NAME).withKey(key);
			DeleteItemResult result = dynamoDB.deleteItem(deleteItemRequest);
			if(result!=null)
			{
				status = true;	
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return status;
	}
	//one record based on code and date..
	public LinearRegression query(String code,String date)
	{
		LinearRegression lr = null;
		Condition rangeKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.EQ.toString())
		.withAttributeValueList(new AttributeValue().withS(date));
		
		QueryRequest queryRequest = new QueryRequest().withTableName(CONFIG.LINEAR_REGRESSION_TABLE_NAME)
				.withHashKeyValue(new AttributeValue().withS(code))
				.withRangeKeyCondition(rangeKeyCondition);
		QueryResult result = dynamoDB.query(queryRequest);
		if(result != null && result.getItems().size() >0)
		{
			Map<String, AttributeValue> item = result.getItems().get(0);
			lr = convert(item);
			System.out.println(item);
		}
		
		return lr;
	}
	
	
	public LinearRegression convert(Map<String, AttributeValue> item)
	{
		LinearRegression lr = null;
		String code  = item.get("code").getS();
		String date =  item.get("date").getS();
		double slope = new Double(item.get("slope").getN()).doubleValue();
		double intercept = new Double(item.get("intercept").getN()).doubleValue();
		lr = new LinearRegression(code,date,slope,intercept);
		return lr;
	}

}
